﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

<script type="text/javascript">
    $(function() {
        $(".form_datepicker").datepicker();
        intityUserTree_wfstart();
    })

    //统计tree上选中的人员，将其组成一个字符串数组存放在 id=selectarr 隐藏标签中
    function onUsertreeCheck_WF(event, treeId, treeNode) {
        //alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo_wfstart");
        var nodes = treeObj.getCheckedNodes(true);
        var temp = "";
        for (i = 0; i < nodes.length; i++) {
            temp = temp + nodes[i].id + ",";
        }
        $("#selectarr").attr("value", temp);
    };

    var complete = false;
    function getTable(btnTag) {
        var hidValue;
        var txtValue = "";
        complete = true;

        var tableTag = $(btnTag).parent();
        var childTag = $(tableTag).children();

        childTag.each(function() {
            var cls = $(this).attr("class");
            if (cls == "txtTable txtunderline") {
                var txt = $(this).val();
                var name = $(this).attr("name");
                if (txt == "")
                    complete = false;
                txtValue += txt + ";";
            }
            else if (cls == "hidTable")
                hidValue = $(this).val();
        });

        //        if (!complete) {
        //            alert("必须填写完整条件");
        //            return;
        //        }
        //alert(txtValue);
        $.ajax({
            type: "POST",
            url: "/Form/QueryTable",
            data: { "txtValue": txtValue, "sqlValue": hidValue },
            success: function(data) {
                if (data == "none") {
                    alert("无所需查询结果");
                    complete = false;
                } else {
                    complete = true;
                    $("#updateTable").html(data);
                }
            }
        });
    };

    function OnSubmit() {

        var flag, msg;
        var table = document.getElementById("updateTable");
        var tableId = $(table).attr("id");
        if (tableId == 'updateTable') {
            if (!complete) {
                var count = $(".txtTable").length;
                if (count == 0)
                    flag = true;
                else
                    flag = false;
            } else {
                flag = true;
            }
        } else {
            flag = true;
        }

        //        if ($("#wf_name").val() == "") {
        //            flag = false;
        //            msg = "请填写流程名";
        //        }
        if ($("#treevis").val() == "T") {
            if ($("#selectarr").val() == "") {
                flag = false;
                msg = "请选择处理人";
            }
        }
        //先取消每个input都要填写的限制
        //        var pages = $(".page");
        //        $.each(pages, function(key, value) {
        //            var txtBox = $(value).children(".txtInput");
        //            txtBox.each(function() {
        //                if ($(this).val() == "") {
        //                    flag = false;
        //                    msg = "表单存在未填写项";
        //                }
        //            });
        //        })

        if (!flag)
            alert(msg);
        else {
            var table = prodTable();
            $('#sqlargs').val(table);
            show_cover();
        }
        return flag;
    }

    // 遍历table获得每个单元格的值
    function prodTable() {
        var tableDiv = $("#updateTable");
        var tbody = $(tableDiv).children().children();
        var trs = $(tbody).children('tr');
        var trCount = trs.length;
        var rows = "";
        var row;
        trs.each(function() {
            row = '';
            $(this).children().each(function() {
                var hidTag = $(this).children('.tbvalget');
                var hidcls = $(hidTag).attr('class');
                if (hidcls != "undefined" && hidcls != "" && hidcls != null && hidcls != "null") {
                    //alert($(hidTag).val());
                    row += $(hidTag).val() + ',';
                }
                if ($(this).children('LABLE').html() == "统计")
                    row += "gather,";
            });
            rows += row + ';';
        })
        return rows;
    }
   
</script>

<script type="text/javascript">
    //初始化文件选择树
    function wfstart_intityFileTree() {
        $.ajax({
            url: "/Home/getFileTree",
            type: "POST",
            data: { type: "person" },
            success: function(fileNodes) {
                //alert(fileNodes.nodes);
                setting.callback.onClick = wfstart_onFiletreeClick;
                var treeObj = $.fn.zTree.init($("#treeDemo_wfstart"), setting, fileNodes);
                $("#treeDemo_wfstart").show();
                var wfappendix = $("#wfappendix").val();
                if (wfappendix != "") {//如果不是空，说明是第二次点击附件按钮，自动把上次选中的文件节点勾选
                    var node = treeObj.getNodesByParam("id", wfappendix, null);
                    treeObj.checkNode(node, true);
                }
            }
        })
    }

    function wfstart_onFiletreeClick(event, treeId, treeNode) {
        // alert(treeNode.tId + ", " + treeNode.name + " , " + treeNode.id + " , " + treeNode.tag);

        //限制只能上传rar
        // if (treeNode.name.replace(/.+\./, "") == "rar") 

        var filterFileExtend = ".rar,.zip,.doc,.xls,.bmp,.png,.jpeg,.gif,.txt,.jpg";
        var fileExtend = treeNode.name.substring(treeNode.name.lastIndexOf('.')).toLowerCase();
        //alert(fileExtend);
        if (filterFileExtend.indexOf(fileExtend) > -1) {
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo_wfstart");
            $("#txt_appendix").attr("value", treeNode.name);
            $("#wfappendix").attr("value", treeNode.id);
            //alert(treeNode.id);
        }

    };

    function ClearAppendix() {
        $("#txt_appendix").attr("value", "");
        $("#wfappendix").attr("value", "");
        intityUserTree_wfstart();
    }

    //初始化人员选择树
    function intityUserTree_wfstart() {
        $.ajax({
            url: "/Home/getUserTree",
            type: "POST",
            success: function(userNodes) {
                setting.callback.onCheck = onUsertreeCheck_wfstart;
                $.fn.zTree.init($("#treeDemo_wfstart"), setting, eval(userNodes));
                var visible = $("#treevis").val();
                // alert(visible);
                if (visible == "T") {
                    $("#treeDemo_wfstart").show();
                }
                else if (visible = "F") {
                    $("#treeDemo_wfstart").hide();
                }
            }
        });
    }

    //统计tree上选中的人员，将其组成一个字符串数组存放在 id=selectarr 隐藏标签中
    function onUsertreeCheck_wfstart(event, treeId, treeNode) {
        //alert(treeNode.id + ", " + treeNode.name + "," + treeNode.checked);
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo_wfstart");
        var nodes = treeObj.getCheckedNodes(true);

        var temp = "";
        for (i = 0; i < nodes.length; i++) {
            temp = temp + nodes[i].id + ",";
        }
        $("#selectarr").attr("value", temp);
    };

    function proRes(ajaxContext) {
        var restag = {};
        try {
            restag = ajaxContext.get_response().get_object();
        } catch (e) {
            alert("程序出现错误，请联系管理员");
            return;  // return if result is not json
        }
        if (restag.res != true) {
            alert(restag.msg);
        }
        else {
            alert("操作成功");
            var b = document.getElementById("workarea-2-2");
            if (b != null) {
                $('#menu_wfcurtasklist').trigger('click');
            }
            $('#menu_worktable').trigger('click');
            $.fn.deltab('2-1-sponsor');
        }
        hide_cover();
    }

    init_bd();
    function init_bd() {
        var page = $("#div_bd_main").children("div").children("#div_bd").children(".page");
        var trans = $(page).children("#EmployeeTransOfficial");
        if ($(trans).html() != "null") {
            var name = $(page).children("#name");
            $(name).unbind("change");
            $(name).bind("change", function() { fill_usr_info(this); });
        }
    }

    function fill_usr_info(tag) {
        var name = $(tag).val();
        var page = $("#div_bd_main").children("div").children("#div_bd").children(".page");
        $.getJSON(
            "/TmpUsr/QueryUser",
            { name: name },
            function(jsonData) {
                if (jsonData.name != "") {
                    $(page).children("#dept").val(jsonData.dept);
                    $(page).children("#duty").val(jsonData.duty);
                    $(page).children("#gender").val(jsonData.gender);
                    $(page).children("#birthday").val(jsonData.birthday);
                    $(page).children("#height").val(jsonData.height);
                    $(page).children("#race").val(jsonData.race);
                    $(page).children("#politics").val(jsonData.politics);
                    $(page).children("#marrige").val(jsonData.marrige);
                    $(page).children("#schooling").val(jsonData.schooling);
                    $(page).children("#major").val(jsonData.major);
                    $(page).children("#identityCard").val(jsonData.identityCard);
                    $(page).children("#phone").val(jsonData.phone);
                    $(page).children("#revealed").val(jsonData.revealed);
                    $(page).children("#address").val(jsonData.address);
                    $(page).children("#timeIn").val(jsonData.timeIn);
                    $(page).children("#salaryTry").val(jsonData.salaryTry);
                    $(page).children("#salaryOfficial").val(jsonData.salaryOfficial);
                }
            }
        );
    }

    function get_tmp_usr() {
        var main_form = $("#div_bd_main");
        var elemTrans = $(main_form).children("div").children("#div_bd").children(".page").children("#EmployeeTransOfficial").html();
        var result = "";
        result += $("#name").val() + ",";
        result += $("#dept").val() + ",";
        result += $("#duty").val() + ",";
        result += $("#gender").val() + ",";
        result += $("#birthday").val() + ",";
        result += $("#height").val() + ",";
        result += $("#race").val() + ",";
        result += $("#politics").val() + ",";
        result += $("#marrige").val() + ",";
        result += $("#schooling").val() + ",";
        result += $("#major").val() + ",";
        result += $("#identityCard").val() + ",";
        result += $("#phone").val() + ",";
        result += $("#revealed").val() + ",";
        result += $("#address").val() + ",";
        result += $("#timeIn").val() + ",";
        result += $("#salaryTry").val() + ",";
        result += $("#salaryOfficial").val() + "";
        $.ajax({
            type: "POST",
            url: "/TmpUsr/AddUser",
            data: { "tmpuser": result }
        })
    }
</script>

<div id="wfstart" style="padding-top: 35px;">
    <div id="menus">
        <%using (Ajax.BeginForm("DoWFStart", "WorkFlow", new AjaxOptions { UpdateTargetId = "temp", OnSuccess = "proRes" }))
          { %>
        <br />
        <div id="div_bd_main">
            <div style="width: 650px; float: left; margin-left: 10px; background-color: #F9F9F9;
                border: solid 1px #6C92AD;">
                <div id="div_bd" style="position: relative; height: 800px; width: 600px; margin-left: 13px;
                    overflow: auto; padding: 5px;">
                    <% Response.Write(ViewData["formdefi"]); %>
                </div>
                <div class="div_hide">
                    <%=Html.Hidden("user_id", ViewData["user_id"])%>
                    <%=Html.Hidden("form_id", ViewData["form_id"])%>
                    <%=Html.Hidden("wftemplate_id", ViewData["wftemplate_id"])%>
                    <%=Html.Hidden("wf_id", ViewData["wf_id"])%>
                    <%=Html.Hidden("selectarr", ViewData["clrs"])%>
                    <%=Html.Hidden("treedefi", ViewData["treedefi"])%>
                    <%=Html.Hidden("treevis", ViewData["treevisible"])%>
                    <%=Html.Hidden("sqlargs")%>
                </div>
            </div>
            <%--            <div style="float: left; height: 800px; width: 340px;">
                <div style="width: 340px; height: 500px; float: left; padding: 0 0 0 10px;">
                    <div id="div_bd_tail" style="width: 300px; height: 150px; float: left;">--%>
            <div style="float: left; height: 800px; width: 310px; margin-left: 40px;">
                <div style="width: 300px; height: 500px; float: left; position: fixed; top: 130px;
                    left: 950px;">
                    <div id="div_bd_tail" style="width: 300px; height: 60px; float: left;">
                        <div style="width: 100%; display: none;">
                            <div style="width: 35%;">
                                审批建议：
                            </div>
                            <div style="width: 75%;">
                                <%=Html.TextArea("ins_descn", "无",new { style = "width:250px;border:solid 1px #6C92AD;" })%>
                            </div>
                        </div>
                        <div style="width: 100%;">
                            <div style="width: 18%; float: left;">
                                附件名：
                            </div>
                            <div style="width: 80%; float: left;">
                                <input type="text" id="txt_appendix" style="width: 207px; border: solid 1px #6C92AD;" /><%=Html.Hidden("wfappendix")%>
                            </div>
                        </div>
                        <br />
                        <div style="width: 100%;">
                            <div style="width: 33%; float: left;">
                                <input class="btn_style" type="button" value="上传文件" onclick="wfstart_intityFileTree()" /></div>
                            <div style="width: 50%; float: left;">
                                <input class="btn_style" type="button" value="清空附件" onclick="ClearAppendix()" /></div>
                        </div>
                    </div>
                    <div id="div_tree" class="ztreeDemo_wfstartBackground left" style="float: left;">
                        <ul id="treeDemo_wfstart" class="ztree" style="height: 230px; width: 260px; float: left;">
                        </ul>
                    </div>
                    <div style="float: left; width: 100%;">
                        <input class="btn_style" type="submit" value="完成并发送" onclick="return OnSubmit()" />
                    </div>
                </div>
            </div>
        </div>
        <%} %>
    </div>
</div>
